<?php

require_once "init.inc.php";

/*
$query = "PREFIX aiiso: <http://purl.org/vocab/aiiso/schema#>
PREFIX resource: <http://purl.org/vocab/resourcelist/schema#>
PREFIX sioc: <http://rdfs.org/sioc/spec/>

select distinct ?pred ?s
where
{
  ?s a resource:List .
    optional { ?x ?y ?z } .
   #filter( bound(?z+1-3*7/2) ) .
   filter( bound(?z) && ( ?z<10 || ?z=<http://example.com/> ) ) .
    ?s ?pred ?obj ; ?p2 'fred'
 
}
order by ?s desc(?obj)
limit 5
offset 5
";
*/

$query = "select ?subjectUri ?subjectName
where {
 ?subjectUri a <http://www.w3.org/2004/02/skos/core#Concept> .
 ?subjectUri <http://www.w3.org/2004/02/skos/core#prefLabel> ?subjectName .
 ?subjectUri <http://www.w3.org/2004/02/skos/core#inScheme> <http://talisaspire.com/subjects/> .
}
order by ?subjectName";

/*
// this one must be using a different store
$query = "DESCRIBE ?listUri <http://purl.org/vocab/resourcelist/schema#contains> ?s
WHERE
{
  ?listUri <http://purl.org/vocab/resourcelist/schema#contains> ?itemUri .
  ?itemUri <http://purl.org/vocab/resourcelist/schema#resource> <http://www.readinglists.manchester.ac.uk/resources/AB7CEDEF-066A-2027-C176-1AC33C7D40C5> .
}";
*/

$parser = ARC2::getSPARQLParser();

/* parse a query */
$parser->parse($query);
if (!$parser->getErrors()) {
  $q_infos = $parser->getQueryInfos();
  //print_r($q_infos);
}
else {
  echo "invalid query: " . print_r($parser->getErrors(),true) . "\n";
}

	
function prettyPrintQuery($queryInfo) {
	$pp = new PrettyPrintQuery($queryInfo);
	return $pp->getQuery();
}

function getPredicateStatistics($predicate)
{
	$schemaStats = SchemaStats::getTheSchemaStats();
	return $schemaStats->getPredicateStatistics($predicate);
}

// $statistics = getPredicateStatistics("http://talisaspire.com/searchTerms/schema#nodeTitle");
// storePredicateStatistcs("http://talisaspire.com/searchTerms/schema#nodeTitle", $statistics);
// $statistics = getStoredPredicateStatistics("http://talisaspire.com/searchTerms/schema#nodeTitle");

//$statistics = getPredicateStatistics("http://talisaspire.com/searchTerms/schema#nodeTitle");
//print_r($statistics);

//$statistics2 = getPredicateStatistics("http://talisaspire.com/searchTerms/schema#nodeCode");
//print_r($statistics2);


//$statistics = getPredicateStatistics("http://talisaspire.com/schema#listReferences");
//$statistics = getPredicateStatistics("http://talisaspire.com/schema#list");
//$statistics = getPredicateStatistics("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
//$statistics = getPredicateStatistics("http://www.w3.org/2004/02/skos/core#prefLabel");
//$statistics = getPredicateStatistics("http://www.w3.org/2004/02/skos/core#inScheme");

//print_r($statistics);

echo"----  query  ----\n";
echo $query ."\n";

echo"----  optimise  ----\n";
$optimiser = new Optimiser(SchemaStats::getTheSchemaStats());


$queryInfo = $parser->getQueryInfos();
$optimised = $queryInfo;

$optimised = $optimiser->optimise($queryInfo);

if ( $optimiser->status == 'FAIL' ) {
	echo"----  optimising failed :-(  ----\n";
	echo implode("\n",$optimiser->reason) ."\n";
	echo"----  ----  ----\n";
} else {
	//echo"----  optimised query  ----\n";
	//echo print_r($optimised,true) ."\n";
	echo"----  optimised query  ----\n";
	echo prettyPrintQuery($optimised) ."\n";
	echo"----  ----  ----\n";
}


?>